<?php

/***
 * Cogsworth
 *
 * An open source multi-application MVC-based framework written in PHP with XML and XSL in mind.
 *
 * @package Cogsworth
 * @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
 * @copyright Copyright (c) 2008, Daniel Wilhelm II Murdoch
 * @link http://www.getcogsworth.com
 * @since Version 1.0
 * @filesource
 ***/

/***
 * CW_ObserverClass
 *
 * This class allows you to monitor elapsed time within profile points.
 *
 * @package Cogsworth
 * @category Core Library
 * @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
 * @copyright Copyright (c) 2008, Daniel Wilhelm II Murdoch
 * @link http://www.getcogsworth.com
 * @since Version 1.0
 ***/

final class CW_ObserverClass {


   /**
	* Core registry
	* @access Protected
	* @var Object
	*/
	static private $observers = array();


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* XML to the current schema.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	static public function register($observer, $executor)
	{
		if(false == function_exists($executor))
		{
			return false;
		}

		if(false == isset(self::$observers[$observer]))
		{
			self::clear($observer);
		}

		if(false == in_array($executor, self::$observers[$observer]))
		{
			self::$observers[$observer][] = $executor;
		}

		return true;
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* XML to the current schema.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	static public function stop($observer, $executor_to_stop)
	{
		// Is this observer even set?

		if(false == self::get($observer))
		{
			return false;
		}

		foreach(self::get($observer) as $key => $executor)
		{
			if($executor_to_stop == $executor)
			{
				unset(self::$observers[$observer][$key]);
			}
		}

		return true;
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* XML to the current schema.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	static public function clear($observer)
	{
		self::$observers[$observer] = array();

		return true;
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* XML to the current schema.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	static public function get($observer)
	{
		return isset(self::$observers[$observer]) ? self::$observers[$observer] : array();
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* XML to the current schema.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	static public function notify($observer)
	{
		// Fetch all the arguments passed through to this notification:

		$arguments = func_get_args();


		// The first argument will always be the name of the observer,
		// so let's just drop it from the list of arguments:

		unset($arguments[0]);


		// Loop through all the executors attached to the specified
		// observer and fire them off while passing through our list
		// of arguments:

		foreach(self::get($observer) as $executor)
		{
			call_user_func_array($executor, $arguments);
		}

		return true;
	}
}


?>